package com.pshcp.service;

import com.framework.thread.ReqComHold;
import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.PatientFavorDTO;
import com.pshcp.domain.model.PatientFavor;
import com.pshcp.mapper.PatientFavorMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 患者关注表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class PatientFavorService {

    @Autowired
    private PatientFavorMapper patientFavorMapper;

    /**
     * 关注数量
     * @param doctorId
     * @return
     */
    public int doctorFavorNum(Long doctorId){
        PatientFavorDTO patientFavorDTO = new PatientFavorDTO();
        patientFavorDTO.setDoctorId(doctorId);
        patientFavorDTO.setStatus(CommonStatusEnum.INIT.getCode());
        return listCount(patientFavorDTO);
    }

    /**
     * 关注
     * @param doctorId
     * @return
     */
    public boolean patientUserIsFavor(Long doctorId){
        PatientFavorDTO patientFavorDTO = new PatientFavorDTO();
        patientFavorDTO.setDoctorId(doctorId);
        patientFavorDTO.setPatientUserId(ReqComHold.getReqCom().getUserId());
        patientFavorDTO.setStatus(CommonStatusEnum.INIT.getCode());
        return listCount(patientFavorDTO) > 0;
    }

    public PageList<PatientFavor> list(PatientFavorDTO patientFavorDTO){
        if(patientFavorDTO.getPageNum() != null && patientFavorDTO.getPageSize() != null){
            PageHelper.startPage(patientFavorDTO.getPageNum(), patientFavorDTO.getPageSize());
        }
        Example example = new Example(PatientFavor.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientFavorDTO.getStatus() != null){
            criteria.andEqualTo("status", patientFavorDTO.getStatus());
        }
        if(patientFavorDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", patientFavorDTO.getDoctorId());
        }
        if(patientFavorDTO.getPatientUserId() != null){
            criteria.andEqualTo("patientUserId", patientFavorDTO.getPatientUserId());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientFavor> list = patientFavorMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public int listCount(PatientFavorDTO patientFavorDTO){
        Example example = new Example(PatientFavor.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientFavorDTO.getStatus() != null){
            criteria.andEqualTo("status", patientFavorDTO.getStatus());
        }
        if(patientFavorDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", patientFavorDTO.getDoctorId());
        }
        if(patientFavorDTO.getPatientUserId() != null){
            criteria.andEqualTo("patientUserId", patientFavorDTO.getPatientUserId());
        }
        return patientFavorMapper.selectCountByExample(example);
    }

    public List<PatientFavor> select(PatientFavor patientFavor){
        return patientFavorMapper.select(patientFavor);
    }

    public int insert(PatientFavor patientFavor){
        patientFavor.setId(SystemUtil.getId());
        patientFavor.setStatus(CommonStatusEnum.INIT.getCode());
        patientFavor.setCreatedTime(new Date());
        return patientFavorMapper.insert(patientFavor);
    }

    public int update(PatientFavor patientFavor){
        patientFavor.setUpdateTime(new Date());
        return patientFavorMapper.updateByPrimaryKeySelective(patientFavor);
    }

    public PatientFavor findById(Long id){
        return patientFavorMapper.selectByPrimaryKey(id);
    }

    public List<PatientFavor> findByIds(List<Long> ids){
        Example example = new Example(PatientFavor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientFavorMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(PatientFavor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientFavor patientFavor = new PatientFavor();
        patientFavor.setStatus(CommonStatusEnum.DEL.getCode());
        return patientFavorMapper.updateByExampleSelective(patientFavor, example);
    }

    public int delByDoctorId(Long doctorId){
        Example example = new Example(PatientFavor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);

        PatientFavor patientFavor = new PatientFavor();
        patientFavor.setStatus(CommonStatusEnum.DEL.getCode());
        return patientFavorMapper.updateByExampleSelective(patientFavor, example);
    }

}
